package com.cangshukj.web.controller.junong;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.cangshukj.junong.domain.LandPics;
import com.cangshukj.junong.domain.SalesType;
import com.cangshukj.junong.service.ILandPicsService;
import com.cangshukj.junong.service.ISalesTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cangshukj.common.annotation.Log;
import com.cangshukj.common.core.controller.BaseController;
import com.cangshukj.common.core.domain.AjaxResult;
import com.cangshukj.common.enums.BusinessType;
import com.cangshukj.junong.domain.SalesService;
import com.cangshukj.junong.service.ISalesServiceService;
import com.cangshukj.common.utils.poi.ExcelUtil;
import com.cangshukj.common.core.page.TableDataInfo;

/**
 * 销告服务服务信息Controller
 * 
 * @author wangjie
 * @date 2025-04-01
 */
@Api(value = "/sale/service",description = "销告服务服务信息")
@RestController
@RequestMapping("/sale/service")
public class SalesServiceController extends BaseController
{
    @Autowired
    private ISalesServiceService salesServiceService;
    @Autowired
    private ILandPicsService landPicsService;
    @Autowired
    private ISalesTypeService salesTypeService;


    /**
     * 查询销告服务服务信息列表
     */
    @ApiOperation("查询销告服务服务信息列表")
    @PreAuthorize("@ss.hasPermi('sale:service:list')")
    @GetMapping("/list")
    public TableDataInfo list(SalesService salesService)
    {
        startPage();
        List<SalesService> list = salesServiceService.selectSalesServiceList(salesService);
        if (list.size() > 0){
            for (SalesService service:list){
                String typeId = service.getTypeId();
                SalesType salesType = salesTypeService.selectSalesTypeByTypeId(typeId);
                service.setTypeName(salesType.getTypeName());
            }
        }
        clearPage();
        return getDataTable(list);
    }



    /**
     * 获取销告服务服务信息详细信息
     */
    @ApiOperation("获取销告服务服务信息详细信息")
    @PreAuthorize("@ss.hasPermi('sale:service:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        SalesService salesService = salesServiceService.selectSalesServiceById(id);
        String dataid = salesService.getId();
        String tablename = "sales_service";
        List<LandPics>  pics = landPicsService.selectLandPicsByFile(dataid,tablename);
        salesService.setSales(pics);
        return AjaxResult.success(salesService);
    }

    /**
     * 新增销告服务服务信息
     */
    @ApiOperation("新增销告服务服务信息")
    @PreAuthorize("@ss.hasPermi('sale:service:add')")
    @Log(title = "销告服务服务信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SalesService salesService)
    {
        salesService.setCreateBy(getUsername());
        salesService.setCreateTime(new Date());
        String typeId = salesService.getTypeId();
        SalesType salesType = salesTypeService.selectSalesTypeByTypeId(typeId);
        salesService.setPid(salesType.getPid());
        salesServiceService.insertSalesService(salesService);
        List<LandPics> sales = salesService.getSales();
        for (LandPics pic:sales){
            LandPics p = new LandPics();
            p.setCreateTime(new Date());
            p.setCreateBy(getUsername());
            p.setDataid(salesService.getId());
            p.setTablename("sales_service");
            p.setFieldname("service_imgs");
            p.setFileName(pic.getFileName());
            p.setFilePath(pic.getFilePath());
            landPicsService.insertLandPics(p);
        }
        return AjaxResult.success();
    }

    /**
     * 修改销告服务服务信息
     */
    @ApiOperation("修改销告服务服务信息")
    @PreAuthorize("@ss.hasPermi('sale:service:edit')")
    @Log(title = "销告服务服务信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SalesService salesService)
    {

        String dataid = salesService.getId();
        List<LandPics> sales = salesService.getSales();
        if (sales.size() > 0){
            landPicsService.deleteLandPicsByDataId(dataid);
            for (LandPics pic:sales){
                LandPics p = new LandPics();
                p.setCreateTime(new Date());
                p.setCreateBy(getUsername());
                p.setDataid(salesService.getId());
                p.setTablename("sales_service");
                p.setFieldname("service_imgs");
                p.setFileName(pic.getFileName());
                p.setFilePath(pic.getFilePath());
                landPicsService.insertLandPics(p);
            }
        }
        String typeId = salesService.getTypeId();
        SalesType salesType = salesTypeService.selectSalesTypeByTypeId(typeId);
        salesService.setPid(salesType.getPid());
        salesService.setUpdateBy(getUsername());
        salesService.setUpdateTime(new Date());
        return toAjax(salesServiceService.updateSalesService(salesService));
    }

    /**
     * 删除销告服务服务信息
     */
    @ApiOperation("删除销告服务服务信息")
    @PreAuthorize("@ss.hasPermi('sale:service:remove')")
    @Log(title = "销告服务服务信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(salesServiceService.deleteSalesServiceByIds(ids));
    }
}
